package com.tg.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.appbase.custom.base.SettingData;
import com.tg.app.R;

import java.util.ArrayList;

public class SettingAdapter extends BaseAdapter {
    private final ArrayList<SettingData> list;
    private final Context context;
    private boolean isCheckButton  = true;
    private  int iconRes = 0;
    private boolean customText = false;
    private  boolean isFirstNormal = false;

    public void setFirstNormal(boolean firstNormal) {
        isFirstNormal = firstNormal;
    }

    public void setItemTextSize(float itemTextSize) {
        this.itemTextSize = itemTextSize;
    }

    private float itemTextSize;

    public void setMarkTextSize(float markTextSize) {
        this.markTextSize = markTextSize;
    }

    private float markTextSize;

    public void setCustomText(boolean customText) {
        this.customText = customText;
    }

    public void setFlag(int position){
        for (int i = 0; i < list.size(); i++){
            SettingData settingData = list.get(i);
            if (i == position){
                settingData.setFlag(1);
            }else{
                settingData.setFlag(0);
            }
        }
    }

    public SettingAdapter(ArrayList<SettingData> list,Context context){
        this.list = list;
        this.context = context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public void setCheckButton(boolean checkButton) {
        isCheckButton = checkButton;
    }

    public void setIconImageResource(int iconRes) {
        this.iconRes = iconRes;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            if (position == 0 && list.size() == 1){
                convertView = LayoutInflater.from(context).inflate(R.layout.settings_item,null);
            }else if (position == 0&&!isFirstNormal){
                convertView = LayoutInflater.from(context).inflate(R.layout.settings_item_first,null);
            }else if (position == list.size() - 1) {
                convertView = LayoutInflater.from(context).inflate(R.layout.settings_item_end,null);
            }else {
                convertView = LayoutInflater.from(context).inflate(R.layout.settings_item_normal,null);
            }

            holder.item = convertView.findViewById(R.id.item_resolution);
            holder.icon = convertView.findViewById(R.id.imageButton2);
            holder.mark = convertView.findViewById(R.id.settings_item_mark);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();

        }

        SettingData data = list.get(position);
        holder.item.setText(data.getName());
        if (customText){
            holder.item.setTextSize(itemTextSize);
            holder.mark.setTextSize(markTextSize);
        }

        if (isCheckButton){

            if (iconRes>0){
                holder.icon.setImageResource(iconRes);
                if(data.getFlag() == 1){
                    holder.icon.setVisibility(View.VISIBLE);
                }else{
                    holder.icon.setVisibility(View.INVISIBLE);
                }
            }else {
                if(data.getFlag() == 1){
                    holder.icon.setVisibility(View.VISIBLE);
                    holder.icon.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_yes);
                }else{
                    holder.icon.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_no);
                    holder.icon.setVisibility(View.VISIBLE);
                }
            }
        }else {
            holder.icon.setImageResource(R.mipmap.next2);
        }
        if (!TextUtils.isEmpty(data.getMark())){
            holder.mark.setVisibility(View.VISIBLE);
            holder.mark.setText(data.getMark());
        }else {
            holder.mark.setVisibility(View.GONE);
        }

        return convertView;
    }

    public final class ViewHolder {
        public TextView item;
        public ImageView icon;
        TextView  mark;
    }

}
